package com.vadmin.vadminjava.service;


import com.alibaba.fastjson.JSONObject;
import com.vadmin.vadminjava.common.MoneyLogEnum;
import com.vadmin.vadminjava.common.ResultSetToJavaBean;
import com.vadmin.vadminjava.db.DBUtil;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/* ***********************************************************
 *
 * 员工的资金实现类
 *
 * **********************************************************/
@Component
public class WxUserMoneyService {

    org.slf4j.Logger logger = LoggerFactory.getLogger(WxUserMoneyService.class.getName());


    /**
     * 得到用户余额
     * @param sender
     * @param decimal
     * @return
     * @throws Exception
     */
    public String getUserBalanceValue(String sender,Integer decimal) throws Exception {
        DBUtil db = new DBUtil();
        String _balance = "0" ;//默认余额
        String sql = "SELECT  * from user where user_address='"+sender+"'";
        String sqlCreate = "INSERT into user(user_address,game_coin_decimals,update_time)values('"+sender+"',"+decimal+",now())";
        try {
            ArrayList<JSONObject> rows = ResultSetToJavaBean.copyJson(db.executeQuery(sql));
            if(rows.size()>0){//有记录才赋值
                _balance = rows.get(0).getString("balance");
            }else{
                Integer i = db.executeUpdateTOnum(sqlCreate);
                if(i>0){
                    System.out.println("为"+sender+"创建了账号数据");
                }
            }
            db.closeNoErr();

        }catch (Exception e){
            logger.info("为用户 {} 创建账号出错了:{}",sender,sqlCreate);
            throw e;
        }
        return _balance;
    }

    /**
     * 通用用户余额操作
     * @param dbUtil
     * @param sender
     * @param amount 操作数量
     */
    public void userMoneyChange(DBUtil dbUtil,String userId,String amount) throws Exception {

        String sql = "update wxwork_user set money = money + "+(amount)+" where user_id='"+userId+"'";
        logger.info("操作用户余额:"+sql);
        try{
            dbUtil.executeUpdateTOnum(sql);//返回执行数
        }catch (Exception e){
            logger.info("操作用户 {} 的余额出错了{}",userId,sql);
            throw e;
        }finally {

        }
    }

    /**
     * 写入资金明细
     * (2022-03-27 测试通过)
     * @param db
     * @param logParams
     * @return
     * @throws Exception
     */
    public String userMoneyLog(DBUtil db, JSONObject logParams) throws Exception{
        try {
            //取得参数
            String user_id         = logParams.getString("user_id");
            String external_userid = logParams.getString("external_userid");
            String open_id         = logParams.getString("open_id");
            String money_before    = logParams.getString("money_before");
            String money           = logParams.getString("money");
            String money_after     = logParams.getString("money_after");
            String coupon_id       = logParams.getString("coupon_id");
            String coupon_code     = logParams.getString("coupon_code");
            String event           = logParams.getString("event");
            String event_code      = logParams.getString("event_code");
            String type            = logParams.getString("type");
            String type_code       = logParams.getString("type_code");
            String remark          = logParams.getString("remark");

            //插入
            String sql = "INSERT INTO `wxwork_user_money_log` " +
                    "( user_id, external_userid,open_id,money_before, money, money_after, coupon_id,coupon_code, event, event_code, type, type_code, remark, create_time) VALUES" +
                    " ( '"+user_id+"','"+external_userid+"','"+open_id+"', '"+money_before+"', '"+money+"', '"+money_after+"', '"+coupon_id+"','"+coupon_code+"', '"+event+"', '"+event_code+"', '"+type+"', '"+type_code+"', '"+remark+"', NOW())";
            //System.out.println("记录明细：" + sql);
            String i = db.executeUpdateGetAutoIncrement(sql);
            return i;
        } catch (Exception e) {
            throw e;
        }
    }

}
